package com.zxy.permission_stu.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxy.permission_stu.config.AjaxResult;
import com.zxy.permission_stu.entity.Courses;
import com.zxy.permission_stu.entity.StuCour;
import com.zxy.permission_stu.entity.Student;
import com.zxy.permission_stu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lenovo
 * @since 2021-11-30
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;
    @Autowired
    private IStuCourService stuCourService;
    @Autowired
    private IClassesService classesService;
    @Autowired
    private ICollegeService collegeService;
    @Autowired
    private ICoursesService coursesService;
    @Autowired
    private KafkaTemplate kafkaTemplate;


    @RequestMapping("getStudent")
    public AjaxResult getStudent(Student student, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Student> list = studentService.getStudent(student);
        PageInfo<Student> info = new PageInfo<>(list);
        return AjaxResult.ok(info);
    }


    @RequestMapping("addStudent")
    public AjaxResult addStudent(@RequestBody Student student){

        student.setUUId(UUID.randomUUID().toString());

        //发送kafka
        String s = JSON.toJSONString(student);
        ListenableFuture<SendResult<String,String>> send = kafkaTemplate.send("addStudent", "",s);
        /*kafka消息丢失*/
        send.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
            @Override
            public void onFailure(Throwable throwable) {
                System.out.println("消息发送失败");
                kafkaTemplate.send("addStudent", s);
            }

            @Override
            public void onSuccess(SendResult<String, String> stringStringSendResult) {
                System.out.println("消息发送成功");
            }
        });
        return AjaxResult.ok();

    }

    /*导出*/
    @RequestMapping("exportExcel")
    public AjaxResult exportExcel(){
        List<Student> student = studentService.getStudent(null);
        ExcelWriter writer = ExcelUtil.getWriter("D:/picture/stu.xlsx");
        writer.addHeaderAlias("id","ID");
        writer.addHeaderAlias("name","学生名称");
        writer.addHeaderAlias("sex","性别");
        writer.addHeaderAlias("birthday","生日");
        writer.addHeaderAlias("className","班级名");
        writer.addHeaderAlias("collegeName","学院名");
        writer.addHeaderAlias("coursesName","课程名");
        writer.addHeaderAlias("claeeseId","班级ID");
        writer.addHeaderAlias("collegeId","学院ID");
        writer.addHeaderAlias("courses","学院");
        writer.addHeaderAlias("UUId","uuid");

        writer.write(student,true);
        writer.close();
        return AjaxResult.ok();
    }
    /*导入数据库*/
    @RequestMapping("importExcel")
    public AjaxResult importExcel(){
        ExcelReader reader = ExcelUtil.getReader("D:/picture/stu.xlsx");
        reader.addHeaderAlias("ID","id");
        reader.addHeaderAlias("学生名称","name");
        reader.addHeaderAlias("性别","sex");
        reader.addHeaderAlias("生日","birthday");
        reader.addHeaderAlias("班级名","className");
        reader.addHeaderAlias("学院名","collegeName");
        reader.addHeaderAlias("课程名","coursesName");
        reader.addHeaderAlias("班级ID","claeeseId");
        reader.addHeaderAlias("学院ID","collegeId");
        reader.addHeaderAlias("学院","courses");
        reader.addHeaderAlias("uuid","UUId");

        List<Student> students = reader.readAll(Student.class);

        studentService.saveBatch(students);
        reader.close();
        return AjaxResult.ok();
    }


}
